package com.sky.wsp.job.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.wsp.job.dao.vo.ProvinceCityVO;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2024/6/3
 */
public class JsonUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * TypeReference<List<ProvinceCityVO>> typeReference = new TypeReference<List<ProvinceCityVO>>() {};
     */
    public static <T> List<T> parseArray(String json, TypeReference<List<T>> typeReference){
        List<T> list = Collections.emptyList();
        try {
            list = objectMapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * TypeReference<List<ProvinceCityVO>> typeReference = new TypeReference<List<ProvinceCityVO>>() {};
     */
    public static <T> List<T> parseArray(File json, TypeReference<List<T>> typeReference){
        List<T> list = Collections.emptyList();
        try {
            list = objectMapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *
     */
    public static <T> T parse(String json, TypeReference<T> typeReference){
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
